package com.qiantu.phone.ui.dialog;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.base.BaseDialog;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.utils.SPUtils;

import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/12/2
 * desc   : 提示对话框
 */
public final class PermissionsConfirmDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        @Nullable
        private OnListener mListener;

        private final TextView permissionStorage;
        private final TextView permissionLocation;
        private final TextView permissionPhoneState;
        private final TextView tvHint;
        private final TextView btnConfirm;
        private boolean hasAllPermissions = true;
        boolean isFirstApply;

        private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.confirm_permissions_dialog);
            setCancelable(false);
            setCanceledOnTouchOutside(false);
            setWidth(getResources().getDimensionPixelSize(R.dimen.dp_343));
            permissionStorage = findViewById(R.id.permission_storage);
            permissionLocation = findViewById(R.id.permission_location);
            permissionPhoneState = findViewById(R.id.permission_phone_state);
            tvHint = findViewById(R.id.tv_hint);
            btnConfirm = findViewById(R.id.btn_confirm);
            setOnClickListener(btnConfirm);
            isFirstApply = SPUtils.getInstance().getBoolean("isFirstApply", true);
            setPermission();
        }

        public Builder setPermission() {
            if (isFirstApply) {
                permissionStorage.setVisibility(View.VISIBLE);
                permissionLocation.setVisibility(View.VISIBLE);
                permissionPhoneState.setVisibility(View.VISIBLE);
                hasAllPermissions = false;
                tvHint.setText(R.string.first_apply_permission_hint);
                btnConfirm.setText(R.string.i_got_it);
            } else {
                for (String permission : permissions) {
                    if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)
                            && !XXPermissions.isGranted(getContext(), permission)) {
                        permissionStorage.setVisibility(View.VISIBLE);
                        hasAllPermissions = false;
                    } else if (Manifest.permission.ACCESS_COARSE_LOCATION.equals(permission)
                            && !XXPermissions.isGranted(getContext(), permission)) {
                        permissionLocation.setVisibility(View.VISIBLE);
                        hasAllPermissions = false;
                    } else if (Manifest.permission.ACCESS_FINE_LOCATION.equals(permission)
                            && !XXPermissions.isGranted(getContext(), permission)) {
                        permissionLocation.setVisibility(View.VISIBLE);
                        hasAllPermissions = false;
                    } else if (Manifest.permission.READ_PHONE_STATE.equals(permission)
                            && !XXPermissions.isGranted(getContext(), permission)) {
                        permissionPhoneState.setVisibility(View.VISIBLE);
                        hasAllPermissions = false;
                    }
                }
                tvHint.setText(R.string.apply_permission_hint);
                btnConfirm.setText(R.string.go_to_settings);
            }
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @Override
        public BaseDialog create() {
            return super.create();
        }

        public boolean isHasAllPermissions() {
            return hasAllPermissions;
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            int viewId = view.getId();
            if (viewId == R.id.btn_confirm) {
                dismiss();
                if(isFirstApply){
                    XXPermissions.with(getContext()).permission(permissions)
                            .request(new OnPermissionCallback() {
                                @Override
                                public void onGranted(List<String> permissions, boolean all) {

                                }

                                @Override
                                public void onDenied(List<String> permissions, boolean never) {

                                }
                            });
                    SPUtils.getInstance().put("isFirstApply", false);
                }else{
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.setData(Uri.parse("package:" + getContext().getPackageName()));
                    getContext().startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                if (mListener != null) {
                    mListener.onConfirm(getDialog());
                }
            }
        }
    }

    public void launchAppDetailsSettings() {

    }

    public interface OnListener {

        /**
         * 点击确定时回调
         */
        void onConfirm(BaseDialog dialog);
    }
}